<template>
  <el-dialog :title="form.id? '编辑部门':'新增部门'" :visible="showDialog" width="50%" @close="close">
    <!-- 新增部门表单 -->
    <el-form ref="fm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>

    <!-- 弹层底部放置操作按钮的具名插槽 -->
    <span slot="footer">
      <el-button @click="$emit('close-dialog', false)">取消</el-button>
      <el-button type="primary" @click="addDept">{{ form.id? '修改' : '新增' }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    // 父传子
    // 父组件传过来的变量=》控制弹层是否显示
    showDialog: {
      type: Boolean,
      default: false
    },
    parentDept: {
      type: Object,
      default: () => ({})
    },
    allDepts: {
      type: Array,
      default: () => []
    }
  },
  data () {
    const validCode = (rule, value, cb) => {
      let flag
      if (this.form.id) {
        // 编辑=》排除自身
        flag = this.allDepts.some(item => item.code === value && value !== this.parentDept.code)
      } else {
        // 新增
        flag = this.allDepts.some(item => item.code === value)
      }
      if (flag) {
        cb(new Error('部门编码重复了'))
      }
      cb()
    }
    return {
      peoples: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.hGetEmployeeSimple()
  },
  methods: {
    // 获取员工简单列表数据
    async hGetEmployeeSimple () {
      const data = await getEmployeeSimple()
      this.peoples = data
    },
    // 关闭弹层执行
    close () {
      // console.log('弹层关闭了')
      // 通过子传父修改showDialog关闭弹层
      this.$emit('close-dialog', false)
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 清除校验残留
      this.$refs.fm.resetFields()
    },
    // 新增部门
    async addDept () {
      try {
        await this.$refs.fm.validate()
        // 校验通过
        // 调用接口新增
        if (this.form.id) {
          await updateDepartments(this.form)
        } else {
          await addDepartments({ ...this.form, pid: this.parentDept.id || '' })
        }
        // 新增成功获取最新数据，刷新
        this.$emit('refresh-dept')
        // 3. 关闭弹层
        this.$emit('close-dialog', false)
        this.$message.success('操作成功')
      } catch (error) {
        console.log('校验失败', error)
      }
    },
    async getDeptDetil (id) {
      const detail = await getDepartDetail(id)
      this.form = detail
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
